package edu.mtu.citizenscience.mushroommapper.activity.main.nameselect;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.ArrayAdapter;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Name List Adapter adapts names into a searchable name list
 */
public class NameListAdapter extends ArrayAdapter<String> {
	// the list of all names that are searched from
	private List<String> originalNames;
	
	/**
	 * Create the Name List Adapter
	 * 
	 * @param context - Context - the activity that holds the names list adapter
	 * @param names - List<String> - list of names
	 * @return void
	 */
	public NameListAdapter(Context context, List<String> names)
	{
		// create array adapter with simple list text view and add the names list
		super(context, android.R.layout.simple_list_item_1, android.R.id.text1, names);

		// create a new orginal names list for searching through
		this.originalNames = new ArrayList<String>();
		
		// loop over the names
		for (String name: names)
		{
			// add them into the orginal names list
			this.originalNames.add(name);
		}
	}
	
	/**
	 * search the list for the term substring displaying only the 
	 * names containing term
	 * 
	 * @param term - String - the search string to constrain the list by
	 * @return void
	 */
	@SuppressLint("DefaultLocale")
	public void search(String term)
	{
		// clear the list of all items
		this.clear();
		
		// loop over all of the orginial names
		for (String name: originalNames)
		{
			// if the name contains the search term or the term is nothing
			if (name.toLowerCase(Locale.US).contains(term.toLowerCase()) || term.equals(""))
			{
				// add the name to the list
				this.add(name);
			}
		}
	}
	
}
